<?php
/**new
 **/

import('classes.handler.Handler');
class TMTGHandler extends Handler {
    function TMTGHandler()
    {
    }

    function CTTMTG($args, &$request) {
        $xMode  = Request::getUserVar("Mode");
        //echo "$xMode   xMode  ";

        if ($xMode !==null)
            $this->edit_createCTTMTG($request->getBaseUrl(), $args);
        else
            $this->listCTTMTG($request->getBaseUrl());

    }

    function CTTMTG_In($args, &$request) {
        import('classes.acct.doiso');
        $idphieu  = Request::getUserVar("idphieu");
        $nam  = Request::getUserVar("nam");
        $noco  = Request::getUserVar("noco");
        $journal =& Request::getJournal();
        $journalid =$journal->getId();

        $query = "SELECT a.soct, a.ngct, a.daidien, a.diachi, a.lydo, a.TK , b.tkdu , b.sotienvn FROM CTTMTG" .$journalid. $nam . " a LEFT JOIN CTTMTG".$journalid.$nam . "CT b ON (a.idphieu = b.idphieu) ";

        $query = $query . "WHERE a.idphieu = '". $idphieu ."' " ;
        $ctktDao =& DAORegistry::getDAO('CTTMTGDAO');
        $ctkts =& $ctktDao->getCTKT_ThuChi($query, $noco);
        $sotien = floatval($ctkts[0]["sotienvn"]);
        $bangchu = doiso($sotien);

        $templateMgr =& TemplateManager::getManager();
        $templateMgr->assign('soct', $ctkts[0]["soct"]);
        $templateMgr->assign('ngct', $ctkts[0]["ngct"]);
        $templateMgr->assign('daidien', $ctkts[0]["daidien"]);
        $templateMgr->assign('diachi', $ctkts[0]["diachi"]);
        $templateMgr->assign('lydo', $ctkts[0]["lydo"]);
        $templateMgr->assign('solgctg', $ctkts[0]["solgctg"]);
        if ($noco == "N")
        {
            $templateMgr->assign('tenphieu', "Phiếu Thu");
            $templateMgr->assign('tkno', $ctkts[0]['tk']);
            $templateMgr->assign('tkco', $ctkts[0]['tkdu']);
        }
        else
        {
            $templateMgr->assign('tenphieu', "Phiếu Chi");
            $templateMgr->assign('tkco', $ctkts[0]['tk']);
            $templateMgr->assign('tkno', $ctkts[0]['tkdu']);

        }
        //$templateMgr->assign_by_ref('ctkts', $ctkts);
        $templateMgr->assign('noco', $noco);
        $templateMgr->assign('sotien', $sotien);
        $templateMgr->assign('bangchu', $bangchu);
        $templateMgr->display('acctCN/ThuChi.tpl');
    }

    function listCTTMTG($baseurl) {
        $templateMgr =& TemplateManager::getManager();
        $user =& Request::getUser();
        $userId = $user->getId();
        $xma_dvcs = $user->getPath();
        $journal =& Request::getJournal();
        $journalid =$journal->getId();


        $gcNam  = Request::getUserVar("dateExpire1Year");
        $xThang1  = Request::getUserVar("dateExpire1Month");

        if ($xThang1)
        {
            $gcNam  = Request::getUserVar("dateExpire1Year");
            $gcNam2  = Request::getUserVar("dateExpire1Year");
            if ($gcNam !=$gcNam2)
            {
                echo '<script type="text/javascript">alert("Chi bao cao trong khoang thoi gian 1 nam"); </script>';
                return;
            }

            $xNgay1  = Request::getUserVar("dateExpire1Day");
            $xThang1  = Request::getUserVar("dateExpire1Month");
            $xNgay2  = Request::getUserVar("dateExpire2Day");
            $xThang2  = Request::getUserVar("dateExpire2Month");
            $date1 = mktime(0, 0, 0, $xThang1, $xNgay1, $gcNam);
            $date2 = mktime(0, 0, 0, $xThang2, $xNgay2, $gcNam);
            $ngay1 = date ( 'Y-m-d', $date1 );
            $ngay2 = date ( 'Y-m-d', $date2 );
        }
        else
        {
            $ngay1  = Request::getUserVar("ngay1");
            $ngay2  = Request::getUserVar("ngay2");
            if ($ngay1)
            {
                $gcNam = substr($ngay1,0,4);
            }
            else
            {
                $xThang  =date("m", time());//
                $xNgay2  =date("d", time());//
                $xNgay1 = $xNgay2  - 10;
                if ($xNgay1 <=0)
                    $xNgay1 = 1;
                $date1 = mktime(0, 0, 0, $xThang, $xNgay1, $gcNam);
                $date2 = mktime(0, 0, 0, $xThang, $xNgay2, $gcNam);
                $ngay1 = date ( 'Y-m-d', $date1 );
                $ngay2 = date ( 'Y-m-d', $date2 );
            }
        }

        $templateMgr->assign('xNam', $gcNam);

        $templateMgr->assign('Date1', $ngay1);
        $templateMgr->assign('Date2', $ngay2);

        $ctktDao =& DAORegistry::getDAO('CTTMTGDAO');
        $dict = NewDataDictionary($ctktDao->_dataSource);
        $tables = $dict->MetaTables('TABLES', false);
        $tablename = "cttmtg".$journalid. $gcNam;
        if (!in_array($tablename,$tables))
        {
            echo '<script type="text/javascript">alert("Khong co du lieu!"); </script>';
            $oldurl= Request::url(null, "index", "CN_TMTG");
            Request::redirectUrl($oldurl);
        }




        $xChiTiet  = Request::getUserVar("ChiTiet");
        if ($xChiTiet)
        {
            $sttChiTiet =substr($xChiTiet,0,1);

        }
        else
        {
            //$sttChiTiet = Request::getUserVar("sttChiTiet");
        }
        $templateMgr->assign('ChiTiet', $xChiTiet);



        $xmant  = Request::getUserVar("mant");
        $templateMgr->assign('loaitien', $xmant);


        //$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
        switch($sttChiTiet)
        {
            case "1":
                $templateMgr->assign('loaiphieu', "Phi&#7871;u Thu");
                $templateMgr->assign('mact', "1-TTM");
                $templateMgr->assign('ma_dk', "PT1");

                $templateMgr->assign('noco', "N");
                $templateMgr->assign('lTK', "TK N&#7907;");
                $templateMgr->assign('lTKDU', "TK C&#243;");
                if ($xmant == "VND")
                    $templateMgr->assign('loaitk', "1111");
                else
                    $templateMgr->assign('loaitk', "1112");
                break;
            case "2":
                $templateMgr->assign('loaiphieu', "Phi&#7871;u Chi");
                $templateMgr->assign('mact', "2-CTM");
                $templateMgr->assign('ma_dk', "PC1");
                $templateMgr->assign('noco', "C");
                $templateMgr->assign('lTKDU', "TK N&#7907;");
                $templateMgr->assign('lTK', "TK C&#243;");
                if ($xmant == "VND")
                    $templateMgr->assign('loaitk', "1111");
                else
                    $templateMgr->assign('loaitk', "1112");

                break;
            case "3":
                $templateMgr->assign('loaiphieu', "B&#225;o c&#243;/G&#7917;i ti&#7873;n Ng&#226;n h&#224;ng");
                $templateMgr->assign('mact', "3-TTG");
                $templateMgr->assign('ma_dk', "BN1");
                $templateMgr->assign('noco', "N");
                $templateMgr->assign('lTK', "TK N&#7907;");
                $templateMgr->assign('lTKDU', "TK C&#243;");
                if ($xmant == "VND")
                    $templateMgr->assign('loaitk', "1121");
                else
                    $templateMgr->assign('loaitk', "1122");

                break;
            case "4":
                $templateMgr->assign('loaiphieu', "B&#225;o n&#7907;/R&#250;t ti&#7873;n Ng&#226;n h&#224;ng");
                $templateMgr->assign('mact', "4-CTG");
                $templateMgr->assign('ma_dk', "BC1");
                $templateMgr->assign('noco', "C");
                $templateMgr->assign('lTKDU', "TK N&#7907;");
                $templateMgr->assign('lTK', "TK C&#243;");
                if ($xmant == "VND")
                    $templateMgr->assign('loaitk', "1121");
                else
                    $templateMgr->assign('loaitk', "1122");

                break;

        }

        //$templateMgr->assign('mact', $xChiTiet);
        $templateMgr->assign('action', "Chung tu Thang ".$xThang."/".$gcNam);


        // lay so lieu
        // them phan Search

        $searchType = null;
        $searchMatch = null;
        $search = Request::getUserVar('search');
        $searchInitial = Request::getUserVar('searchInitial');
        if (!empty($search)) {
            $searchType = Request::getUserVar('searchField');
            $searchMatch = Request::getUserVar('searchMatch');

        } elseif (!empty($searchInitial)) {
            $searchInitial = String::strtoupper($searchInitial);
            $searchType = USER_FIELD_INITIAL;
            $search = $searchInitial;
        }

        $fieldOptions = Array(
            CTKT_FIELD_TKDU => 'TK &#272;&#244;&#237; &#7913;ng ',
            CTKT_FIELD_SOCT => 'S&#7889; ch&#7913;ng t&#7915; ',
            CTKT_FIELD_DIENGIAI => 'Di&#7877;n gi&#7843;i ',
            CTKT_FIELD_SOTIENVN => 'S&#7889; ti&#7873;n '
        );
        $templateMgr->assign('fieldOptions', $fieldOptions);
        $templateMgr->assign('searchField', $searchType);
        $templateMgr->assign('searchMatch', $searchMatch);
        $templateMgr->assign('search', $search);
        $templateMgr->assign('searchInitial', Request::getUserVar('searchInitial'));



        ///
        $searchTypeMap = array(
            CTKT_FIELD_TKDU => 'TKDU',
            CTKT_FIELD_SOCT => 'A.SOCT',
            CTKT_FIELD_DIENGIAI => 'DIENGIAI',
            CTKT_FIELD_SOTIENVN => 'B.SOTIENVN'
        );
        $paramArray = array();
        $searchSql = '';

        if (!empty($search) && isset($searchTypeMap[$searchType])) {
            $fieldName = $searchTypeMap[$searchType];
            switch ($searchMatch) {
                case 'is':
                    $searchSql = "AND LOWER($fieldName) = LOWER(?)";
                    $paramArray[] = $search;
                    break;
                case 'contains':
                    $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
                    $paramArray[] = '%' . $search . '%';
                    break;
                case 'startsWith':
                    $searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
                    $paramArray[] = $search . '%';
                    break;
            }
        }



        $rangeInfo = Handler::getRangeInfo('ctkts');
        {
            $query = "SELECT a.idphieu, a.soct, a.ngct, b.diengiai, a.mant, b.sotien_tygia, a.TK , a.macttk , b.tkdu , b.macttkdu, b.sotiennt, b.sotienvn FROM CTTMTG".$journalid. $gcNam . " a LEFT JOIN CTTMTG".$journalid. $gcNam . "CT b ON (a.idphieu = b.idphieu) ";

            $query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.mact = '" . $templateMgr->_tpl_vars['mact'] . "' " ;
            $query = $query . $searchSql;
            $query = $query . " ORDER BY ngct, soct";
            $ctkts =& $ctktDao->getCTKT_viewNT($query, $paramArray, $rangeInfo);

        }
        $templateMgr->assign('journalid', $journalid);

        ////
        $templateMgr->assign_by_ref('ctkts', $ctkts);
        //$newurlUpd = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctCN/u_CTTMTG.php";

        $newurlDel = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctCN/d_CTTMTG.php";

        $newurl = Request::url(null, null, 'CTTMTG', null, array('Mode' => 'Edit', 'mant' => $xmant,  'ChiTiet' => $xChiTiet, 'Nam' => $gcNam, "ngay1"=> $ngay1, 'ngay2'=>$ngay2 ));
        $templateMgr->assign('url', $newurl );

        $newurl = Request::url(null, 'acctCN', 'CTTMTG_In', null);
        $templateMgr->assign('urlIn', $newurl );


        //$date1  = Request::getUserVar("fNgay1");
        //$date2  = Request::getUserVar("fNgay2");

        $newurlRFR = Request::url(null, null, 'CTTMTG', null, array('mant' => $xmant, 'noco' => $noco, 'loaitk' =>$loaitk, 'ChiTiet' => $xChiTiet, 'ChiTiet' => $xChiTiet, 'Nam' => $gcNam, "ngay1"=> $ngay1, 'ngay2'=>$ngay2 ));
        $templateMgr->assign('urlRFR', $newurlRFR );

        //$templateMgr->assign('urlUpd', $newurlUpd);
        $templateMgr->assign('urlDel', $newurlDel);

        $CN_SecDetail2 ="Xem";
        $templateMgr->assign('CN_SecDetail2', $CN_SecDetail2);
        $templateMgr->assign('CN_SecDetail', "CN_TMTG");
        //$templateMgr->assign('searchSql', $searchSql);

        $templateMgr->addStyleSheet($baseurl.'/styles/dm.css');
        $templateMgr->addJavaScript("classes/acct/js/ShowHintMATK.js");
        $templateMgr->display('acctCN/CTTMTGList.tpl');
    }


    function createDMTK() {
        $this->editDMTK();
    }

    function RFR_DMDV(){

        $journal =& Request::getJournal();
        $journalid =$journal->getId();;

        $dmdvdao =& DAORegistry::getDAO('DMDVDAO');
        $dmdv_initial = Request::getUserVar("dmdv_initial");;
        $dmdvString = $dmdvdao->getSetting($journalid, $dmdv_initial);
        echo $dmdvString ;
    }




//	function getTen_DMTK(){
//		$journal =& Request::getJournal();
//		$journalid =$journal->getId();;
//		$dmtkdao =& DAORegistry::getDAO('DMTKDAO');
//
//		$dmtk_ma = Request::getUserVar("dmtk_matk");
//		if (strlen($dmtk_ma)==0)
//		{
//			echo "";
//			return;
//
//		}
//		$dmtk_ma .= '</a></b>-';
//		$dmtkString = $dmtkdao->getSetting($journalid, 'tracuu');
//		echo between ($dmtk_ma, '<br><b>', $dmtkString );
//
//	}




    function edit_createCTTMTG($baseurl, $args)
    {
        $this->validate();
        $this->setupTemplate(true);
        $journal =& Request::getJournal();
        $journalid =$journal->getId();


        import('classes.acct.form.CTTMTGForm');

        $CTTMTGForm = new CTTMTGForm(!isset($args) || empty($args) ? null : ( $args[0]));

        if ($CTTMTGForm->isLocaleResubmit()) {
            $CTTMTGForm->readInputData();
        } else {
            $CTTMTGForm->initData();
        }
        $templateMgr =& TemplateManager::getManager();

        $user =& Request::getUser();
        $ma_dvcs  = Request::getUserVar("ma_dvcs");
        $templateMgr->assign('ma_dvcs',  $ma_dvcs);     // dùng để tra cứu


        $dmtkDao =& DAORegistry::getDAO('DMTKDAO');
        //$dmtkString = $dmtkDao->getSetting($journalid, 'tracuu');
        $matkAr = $dmtkDao->getSetting($journalid, 'matk');
        //$matkAr = explode("-", $matkString);
        array_unshift($matkAr, "");

        $templateMgr->assign('matka', $matkAr);


        $dmdvdao =& DAORegistry::getDAO('DMDVDAO');
        //$dmdv_initial = "0";
        //$dmdvString = $dmdvdao->getSetting($journalid, $dmdv_initial);

        $madvAr = $dmdvdao->getSettingMADV($journalid, 'madv');

        array_unshift($madvAr, "");
        $templateMgr->assign('madva',  $madvAr); // dung cho combo

        //$madvAr = $dmdvdao->getSettingList($journalid, 'DMDVList');
        $templateMgr->assign('madvList',  $madvAr);     // dùng để tra cứu


        $xThang  = Request::getUserVar("dateExpire1Month");
        $xNam  = Request::getUserVar("dateExpire1Year");
        if (!$xNam)
            $xNam = Request::getUserVar("Nam");


        ///----------
        $xChiTiet  = Request::getUserVar("ChiTiet");
        $templateMgr->assign('ChiTiet', $xChiTiet);
        $sttChiTiet =substr($xChiTiet,0,1);

        $mant  = Request::getUserVar("mant");
        if (!$mant)
            $mant = "VND";

        $templateMgr->assign('mant', $mant);

        $dmntDao =& DAORegistry::getDAO('DMNTDAO');
        $dsmant =& $dmntDao->getDSMANT_MA();
        $templateMgr->assign('dmntf', $dsmant );


        $dmtkDao =& DAORegistry::getDAO('DMTKDAO');

        switch($sttChiTiet)
        {
            case "1":
                $templateMgr->assign('loaiphieu', "Phi&#7871;u Thu");
                $templateMgr->assign('ma_dk', "PT1");
                $templateMgr->assign('noco', "N");
                $templateMgr->assign('lTK', "TK N&#7907;");
                $templateMgr->assign('lTKDU', "TK C&#243;");
                $dstk =& $dmtkDao->getDSMATK("111");
                break;
            case "2":
                $templateMgr->assign('loaiphieu', "Phi&#7871;u Chi");
                $templateMgr->assign('ma_dk', "PC1");

                $templateMgr->assign('noco', "C");
                $templateMgr->assign('lTKDU', "TK N&#7907;");
                $templateMgr->assign('lTK', "TK C&#243;");
                $dstk =& $dmtkDao->getDSMATK("1111");
                break;
            case "3":
                $templateMgr->assign('loaiphieu', "G&#7917;i ti&#7873;n Ng&#226;n h&#224;ng");
                $templateMgr->assign('ma_dk', "BN1");

                $templateMgr->assign('noco', "N");
                $templateMgr->assign('lTK', "TK N&#7907;");
                $templateMgr->assign('lTKDU', "TK C&#243;");
                $dstk =& $dmtkDao->getDSMATK("112");

                break;
            case "4":
                $templateMgr->assign('loaiphieu', "R&#250;t ti&#7873;n Ng&#226;n h&#224;ng");
                $templateMgr->assign('noco', "C");
                $templateMgr->assign('ma_dk', "BC1");

                $templateMgr->assign('lTKDU', "TK N&#7907;");
                $templateMgr->assign('lTK', "TK C&#243;");

                $dstk =& $dmtkDao->getDSMATK("112");
                break;
        }
        $templateMgr->assign('tktien', $dstk);
        $templateMgr->assign('mact', $xChiTiet);

        $newurl = Request::url(null, null, 'CTTMTG', null, array('Mode' => 'Add', 'mant' => $xmant, 'ChiTiet' => $xChiTiet, 'Nam' => $xNam ));
        $templateMgr->assign('url', $newurl );

        $templateMgr->addJavaScript('lib/pkp/js/lib/jquery/plugins/validate/jquery.validate.min.js');
        $templateMgr->addJavaScript('lib/pkp/js/jqueryValidatorI18n.js');

       $templateMgr->assign('journalid', $journalid);

        $templateMgr->assign('xNam', $xNam);
        $templateMgr->assign('xNamT', $xNam-1);

        $xidPhieu  = Request::getUserVar("idPhieu");
        if ($xidPhieu == null)
        {
            $templateMgr->assign('action', "Th&#234;m ch&#7913;ng t&#7915; m&#7899;i");
        }
        else
        {
            $templateMgr->assign('action', "S&#7917;a ch&#7913;ng t&#7915;");
        }

        $xidPhieu  = Request::getUserVar("idPhieu");
        if ($xidPhieu  !== null)
        {
            $ctktDao =& DAORegistry::getDAO('CTTMTGDAO');

            //$query = "SELECT a.idphieu, a.soct, a.ngct, a.daidien, a.diachi, a.lydo, a.solgctg, b.diengiai, a.mant, b.tygia, a.TK , a.mact, a.noco, b.tygia,  b.tkdu , b.macttkdu,  b.sotienvn , b.sotiennt, b.id  FROM CTTMTG".$journalid.$xNam . " a LEFT JOIN CTTMTG".$journalid . $xNam . "CT b ON (a.idphieu = b.idphieu)  where a.idphieu = '". $xidPhieu. "' order by a.idphieu, b.id";
//nhat----------
            $query = "SELECT a.*, b.*, c.*  FROM CTTMTG".$journalid.$xNam . " a LEFT JOIN CTTMTG".$journalid . $xNam . "CT b ON a.idphieu = b.idphieu LEFT JOIN CTTMTG".$journalid . $xNam . "THUE c ON a.idphieu = c.idphieu   where a.idphieu = '". $xidPhieu. "' order by a.idphieu, b.id, c.idthue";

            $ctkts =& $ctktDao->getCTKT_edit($query);

            $i=0;
            $aCtkt_tkdu = array();
            $aCtkt_macttkdu = array();
            $aCtkt_sotienvn = array();
            $aCtkt_sotiennt = array();
            $aCtkt_diengiai = array();
            $aCtkt_noidungthue = array();
            $aCtkt_tenkh = array();
            $aCtkt_lydo = array();
            $aCtkt_diachi = array();
            $aCtkt_daidien = array();
            //nhat
            $aCtkt_soctcn = array();
            $aCtkt_macp = array();

            $ngay1  = Request::getUserVar("ngay1");
            $ngay2  = Request::getUserVar("ngay2");

            foreach ($ctkts as $key=>$row)
            {
                if ($i==0)
                {
                    $soct = $row["soct"];
                    $ngct = $row["ngct"];
                    $ngaylapct = $row["ngaylapct"];
                    $daidien =  $row["daidien"];
                    $diachi= $row["diachi"];
                    $lydo = $row["lydo"];
                    $noidungthue = $row["noidungthue"];
                    $solgctg= $row["solgctg"];
                    $sotien_tygia = $row["sotien_tygia"];
                    $mant= $row["mant"];
                    $ma_dvcs= $row["ma_dvcs"];
                    $matk= $row["tk"];
                    //nhat
                    $soctcn= $row["soctcn"];
                    $sotienvn = $row["sotienvn"];
                    $sotiennt = $row["sotiennt"];
                    $macp= $row["macp"];
                    $diengiai= $row["diengiai"];
                    $makh= $row["makh"];
                    $tenkh= $row["tenkh"];
                    $tenhh= $row["tenhh"];
                    $loaihinh= $row["loaihinh"];
                    $kihieumd= $row["kihieumd"];
                    $sohd= $row["sohd"];
                    $soseri= $row["soseri"];
                    $ngayhd= $row["ngayhd"];
                    $masot= $row["masot"];
                    $mathue= $row["mathue"];
                    $thuesuat= $row["thuesuat"];
                    $sotien= $row["sotien"];
                    $thue= $row["thue"];
                    $tongtien= $row["tongtien"];
                    $mavuviec= $row["mavuviec"];
                    //end


                    $templateMgr->assign('soct', $soct);
                    $templateMgr->assign('ngct', $ngct);
                    $templateMgr->assign('ngaylapct', $ngaylapct);
                    $templateMgr->assign('daidien', $daidien);
                    $templateMgr->assign('noidungthue', $noidungthue);
                    $templateMgr->assign('diachi', $diachi);
                    $templateMgr->assign('lydo', $lydo);
                    $templateMgr->assign('solgctg', $solgctg);
                    $templateMgr->assign('sotien_tygia', $sotien_tygia);
                    $templateMgr->assign('mant', $mant);
                    $templateMgr->assign('ma_dvcs', $ma_dvcs);
                    $templateMgr->assign('matk', $matk);
                    //nhat
                    $templateMgr->assign('soctcn', $soctcn);
                    $templateMgr->assign('sotienvn', $sotienvn);
                    $templateMgr->assign('sotiennt', $sotiennt);
                    $templateMgr->assign('macp', $macp);
                    $templateMgr->assign('diengiai', $diengiai);
                    $templateMgr->assign('makh', $makh);
                    $templateMgr->assign('tenkh', $tenkh);
                    $templateMgr->assign('tenhh', $tenhh);
                    $templateMgr->assign('loaihinh', $loaihinh);
                    $templateMgr->assign('kihieumd', $kihieumd);
                    $templateMgr->assign('sohd', $sohd);
                    $templateMgr->assign('soseri', $soseri);
                    $templateMgr->assign('ngayhd', $ngayhd);
                    $templateMgr->assign('masot', $masot);
                    $templateMgr->assign('thuesuat', $thuesuat);
                    $templateMgr->assign('sotien', $sotien);
                    $templateMgr->assign('thue', $thue);
                    $templateMgr->assign('mathue', $mathue);
                    $templateMgr->assign('tongtien', $tongtien);
                    $templateMgr->assign('mavuviec', $mavuviec);
                    //end

                }
                $i++;
                $aCtkt_tkdu[] = $row["tkdu"];

                $aCtkt_macttkdu[] = trim($row["macttkdu"]);
                $aCtkt_sotienvn[] = $row["sotienvn"];
                $aCtkt_sotiennt[] = $row["sotiennt"];
                $aCtkt_diengiai[] = $row["diengiai"];
                //nhat
                $aCtkt_noidungthue[] = $row["noidungthue"];
                $aCtkt_tenkh[] = $row["tenkh"];
                $aCtkt_lydo[] = $row["lydo"];
                $aCtkt_diachi[] = $row["diachi"];
                $aCtkt_daidien[] = $row["daidien"];
                $aCtkt_soctcn[] = $row["soctcn"];



            }
            $templateMgr->assign('sodong', $i);
            $sa = implode(",,,", $aCtkt_tkdu);

            $templateMgr->assign('tkduss', $sa);
            $sa = implode(",,,", $aCtkt_macttkdu);

            $templateMgr->assign('macttkduss', $sa);
            $sa = implode(",,,", $aCtkt_sotienvn);

            $templateMgr->assign('sotienvnss', $sa);
            if ($xmant == "VND")
                $templateMgr->assign('sotienntss', -1);
            else
            {
                $sa = implode(",,,", $aCtkt_sotiennt);
                $templateMgr->assign('sotienntss', $sa);
            }


            $sa = implode(",,,", $aCtkt_diengiai);
            $templateMgr->assign('diengiaiss', $sa);
            //nhat
            $sa = implode(",,,", $aCtkt_noidungthue);
            $templateMgr->assign('noidungthuess', $sa);
            $sa = implode(",,,", $aCtkt_soctcn);
            $templateMgr->assign('soctcnss', $sa);

            $sa = implode(",,,", $aCtkt_tenkh);
            $templateMgr->assign('tenkhss', $sa);
            $sa = implode(",,,", $aCtkt_lydo);
            $templateMgr->assign('lydo', $sa);
            $sa = implode(",,,", $aCtkt_diachi);
            $templateMgr->assign('diachi', $sa);
            $sa = implode(",,,", $aCtkt_daidien);
            $templateMgr->assign('daidien', $sa);

            $templateMgr->assign('Mode', 'edit');
            $templateMgr->assign('idphieu', $xidPhieu);

            $CN_SecDetail2 ="Sửa chứng từ";
            //$newurlIns = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctCN/u_CTTMTG.php";
            $newurlIns  = Request::url(null, "acctCN", 'Ins_CT');

            $urlMain = Request::url(null, null, 'CTTMTG', null, array('mant' => $xmant, 'noco' =>  $templateMgr->_tpl_vars['noco'], 'loaitk' => $templateMgr->_tpl_vars['loaitk'], 'ChiTiet' => $xChiTiet, 'ChiTiet' => $xChiTiet, 'Nam' => $xNam, "ngay1"=> $ngay1, 'ngay2'=>$ngay2 ));
        }
        else
        {
            $newurlIns = PKPRequest::getProtocol()."://". PKPRequest::getServerHost().PKPRequest::getBasePath()."/pages/acctCN/i_CTTMTG.php";

            $newurlIns  = Request::url(null, "acctCN", 'Ins_CT');


            $urlMain = Request::url(null, null, 'CTTMTG', null, array('mant' => $xmant, 'noco' =>  $templateMgr->_tpl_vars['noco'], 'loaitk' => $templateMgr->_tpl_vars['loaitk'], 'ChiTiet' => $xChiTiet, 'ChiTiet' => $xChiTiet, 'Nam' => $xNam, 'Mode'=> 'add' ));
            $templateMgr->assign('sodong', 3);
            $CN_SecDetail2 ="Thêm mới chứng từ";
            $templateMgr->assign('Mode', 'add');
        }

        $urlS_DMDV = Request::url(null, 'acctCN', 'RFR_DMDV');
        $urlS_getTenDMDV = Request::url(null, 'acctCN', 'getTen_DMDV');
        $urlS_getTenDMTK = Request::url(null, 'acctCN', 'getTen_DMTK');

        //echo $newurlRFR ;
        $newurlIn = Request::url(null, 'acctCN', 'CTTMTG_In', null);
        $templateMgr->assign('urlIn', $newurlIn );


        $templateMgr->assign('idphieu', $xidPhieu);
        $templateMgr->assign('urlIns', $newurlIns);
        $templateMgr->assign('urlS_DMDV', $urlS_DMDV);
        $templateMgr->assign('urlS_getTenDMDV', $urlS_getTenDMDV);
        $templateMgr->assign('urlS_getTenDMTK', $urlS_getTenDMTK);
        $templateMgr->assign('urlMain', $urlMain);
        //echo $urlMain;

        // thêm ngày cho list
        $xThang  =date("m", time());//
        $xNgay2  =date("d", time());//

        $xNgay1 = $xNgay2  - 10;
        if ($xNgay1 <=0)
            $xNgay1 = 1;
        $date1 = mktime(0, 0, 0, $xThang, $xNgay1, $xNam);
        $date2 = mktime(0, 0, 0, $xThang, $xNgay2, $xNam);
        $ngay1 = date ( 'Y-m-d', $date1 );
        $ngay2 = date ( 'Y-m-d', $date2 );
        $templateMgr->assign('ngay1', $ngay1);
        $templateMgr->assign('ngay2', $ngay2);


        $templateMgr->assign('CN_SecDetail', "CN_TMTG");
        $templateMgr->assign('CN_SecDetail2', $CN_SecDetail2);
        $CTTMTGForm->display();
    }


    function Ins_CT(){
        //dataMain: dataMain, fieldMain: fieldMain, dataCT: dataCT, fieldCT: fieldCT, mact:mact, noco:noco, idphieu: idphieu
        $journalid = Request::getUserVar('journalid') ;
        $Year = Request::getUserVar('year') ;
        $mact = Request::getUserVar('mact') ;
        $noco = Request::getUserVar('noco') ;
        $idphieu = Request::getUserVar('idphieu') ;
        $addmode  = Request::getUserVar("addmode");

        $tableMain = "cttmtg";
        $user =& Request::getUser();
        $userName = $user->getFullName();



        // kiểm tra file:
        $cttmtgDao =& DAORegistry::getDAO('CTTMTGDAO');
        $dict = NewDataDictionary($cttmtgDao->_dataSource);
        $tables = $dict->MetaTables('TABLES', false);
        $tablename = $tableMain.$journalid. $Year;
        if (!in_array($tablename,$tables))
        {
            $cttmtgDao ->CreateTable($Year, $journalid );
        }

        $columns = $dict->MetaColumns($tablename);
        $colnames = array();
        $coltypes = array();
        foreach ($columns as $column) {
            $colnames[] = $column->name;
            $coltypes[] = $column->type;

        }


        $dataMain = Request::getUserVar("dataMain");
        $fieldMain = Request::getUserVar("fieldMain");

        $codulieu = 0;
        $succ = 1;
        $mysql = array();
        $i = 0;

        if ($addmode == "add"){
            $mysqlMain = "INSERT INTO ".$tableMain.$journalid. $Year. " SET mact = '$mact', noco = '$noco',user_name0 = '$userName'" ;

        }
        else{
            $mysqlMain = "UPDATE ".$tableMain.$journalid. $Year. " SET user_name1 = '$userName'" ;

        }

        foreach ($dataMain as $key=>$value) {
            $fdulieu = "$value";
            if (!in_array(strtolower($fieldMain[$key]),$colnames))
            {
                continue;
            }

            if (strpos($mysqlMain, $fieldMain[$key]) <=0){  // field 0 trungf
                if ($fdulieu)
                {
                    if(preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/',$value)){
                        $dt1   = substr($value,0,2);
                        $mon1  = substr($value,3,2);
                        $yr1   = substr($value,6);
                        $value = "$yr1-$mon1-$dt1";
                    }
                    $mysqlMain .= ", ". $fieldMain[$key]."  = '".$value."'";

                }

            }
        }

        if ($addmode == "edit")
            $mysqlMain .= " WHERE idphieu='". $idphieu . "'";

        $ctDao =& DAORegistry::getDAO('CTTMTGDAO');
        echo $mysqlMain;
        $result =& $ctDao->retrieve($mysqlMain);



        if ($addmode == "add") // them moi
        {
            $result =& $ctDao->retrieve("SELECT MAX(idPhieu) as idPhieu FROM ".$tableMain.$journalid. $Year);
            $newidPhieus =$result->GetRowAssoc(false);
            $idphieu = $newidPhieus["idphieu"];

        }
        else
        {
            $mysqld = "DELETE FROM ".$tableMain.$journalid. $Year. "ct WHERE idphieu = '". $idphieu."'";
            $result =& $ctDao->retrieve($mysqld);
        }


        $dataCT = Request::getUserVar("dataCT");
        $fieldCT = Request::getUserVar("fieldCT");

        $columnsct = $dict->MetaColumns($tablename."ct");
        $colnamesct = array();
        $coltypesct = array();
        foreach ($columnsct as $column) {
            $colnamesct[] = $column->name;
            $coltypesct[] = $column->type;
        }

        $mysql = array();
        $i = 0;
        foreach ($dataCT as $v) {
            $mysql[$i] = "INSERT INTO ".$tableMain.$journalid. $Year. "ct SET idPhieu='". $idphieu. "'";
            $codl = null;

            foreach ($v as $key=>$value)
            {
                $fdulieu = "$value";

                if (!in_array(strtolower($fieldCT[$key]),$colnamesct))
                {
                    continue;
                }

                if ($fdulieu)
                {		$mysql[$i] .= ", ". $fieldCT[$key]."  = '".$value."'";
                    $codl = true;
                }
            }
            echo "<br> $mysql[$i]";
            if ($codl){
                $result =& $ctDao->retrieve($mysql[$i]);
                $i++;

            }

        }

        //---------nhat -----
        //thue
        $dataThue = Request::getUserVar("dataThue");
        $fieldThue = Request::getUserVar("fieldThue");
        $columnsctt = $dict->MetaColumns($tablename."thue");
        $colnamesctt = array();
        $coltypesctt = array();
        foreach ($columnsctt as $columnt) {
            $colnamesctt[] = $columnt->name;
            $coltypesctt[] = $columnt->type;
        }
        $mysql = array();
        $i=0;
        foreach ($dataThue as $v) {
            $mysql[$i] = "INSERT INTO ".$tableMain.$journalid. $Year. "thue SET idPhieu='". $idphieu. "'";
            $codl = null;

            foreach ($v as $key=>$value)
            {
                $fdulieu = "$value";

                if (!in_array(strtolower($fieldThue[$key]),$colnamesctt))
                {
                    continue;
                }

                if ($fdulieu)
                {		$mysql[$i] .= ", ". $fieldThue[$key]."  = '".$value."'";
                    $codl = true;
                }
            }
            if ($codl){
                $result =& $ctDao->retrieve($mysql[$i]);
                $i++;

            }

        }
        //---------end--------------



        echo ":::".$idphieu;

    }
}

/**
 * @file TMTGHandler.inc.php
 * Copyright (c) 2003-2012 John Willinsky
 * Copyright (c) VTN.Diep 2014...
 */



?>
